package si.unimb.src.wifisms;

import java.util.ArrayList;

import si.unimb.src.wifisms.control.AsyncDialogSender;
import si.unimb.src.wifisms.control.Database;
import si.unimb.src.wifisms.control.TaskFinishListener;
import si.unimb.src.wifisms.entity.SMSMessage;
import si.unimb.src.wifisms.ui.SMSMessageArrayAdapter;
import si.unimb.src.wifisms.util.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author perox
 * Razred, ki prikaže v MenuList katera sporočila so še na čakanju.
 */
public class WaitingMessages extends Activity implements OnItemClickListener, OnClickListener, TaskFinishListener {

	private ListView listItems;
	private Button posljiVse;
	private ArrayList<SMSMessage> messages;
	private TextView naslov;
	private AsyncDialogSender messageSendingTask;
	private SMSMessage sendMe;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waiting_messages);
        
        posljiVse = (Button)findViewById(R.id.PosljiVseButton);
        messages = Database.getMessages(this);
        
        naslov = (TextView)findViewById(R.id.CakajocaSporocilaText);
        setSporocilaCountText();
        
        try {
	        SMSMessageArrayAdapter caa = new SMSMessageArrayAdapter(this, R.layout.three_line_list_view, messages);
	        this.listItems = (ListView)findViewById(R.id.SporocilaListView);
	        this.listItems.setAdapter(caa);
        } catch (Exception e) {
        	System.out.println(e.getMessage());
        }
        
        this.listItems.setOnItemClickListener(this);
        registerForContextMenu(this.listItems);
        
        posljiVse.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		refreshWaitingMessagesView();
		
	}

	@Override
	public void onClick(View v) {

		if(v.equals(posljiVse)) {

			// Dobimo SMSe za posiljanje
			ArrayList<SMSMessage> messages = Database.getMessages(this);
			
			// Ce ni nic za poslati -> napaka
			if(messages.isEmpty()) {
				
				AlertDialog.Builder adb = new AlertDialog.Builder(this);
				adb.setTitle("Napaka");
				adb.setMessage("Ni sporočil za pošiljanje!");
				adb.setPositiveButton("OK", null);
				adb.show();
				
			} else {
				
				// Drugace pa posljemo sporocila
				messageSendingTask = new AsyncDialogSender(this);
				messageSendingTask.addListener(this);
				messageSendingTask.execute(messages.toArray(new SMSMessage[] {}));
			
			}
			
		}
		
	}
	
	/**
	 * urejanje sporočila, ko kliknemo na eno iz med sporočil
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int id, long arg3) {
		
		Utility.gotoIntent(this, SendSms.class, id);
		
	}
	
	/**
	 * vsebina context menija
	 */
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		
	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	    menu.setHeaderTitle(messages.get(info.position).getMessage()); /* substrings(maxDolžina)...? */
	    
	    String[] contextMenuItems = getResources().getStringArray(R.array.smsContextMenuItems);
	    for (int i = 0; i < contextMenuItems.length; i++) {
	    	menu.add(Menu.NONE, i, i, contextMenuItems[i]);
	    }
	    
    }
	
	/**
	 * kaj se proži ob izbiri elementa context menija
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();

		int id = menuInfo.position;
		if(item.getItemId() == 0) { // urejanje
			
			Utility.gotoIntent(this, SendSms.class, id);
			
		} else if (item.getItemId() == 1) { //izbris
			
			Database.getMessages(this).remove(id);
			Database.persist(this);
			refreshWaitingMessagesView();
			
		} else if (item.getItemId() == 2) { // pošljemo izbran sms
			
			sendMe = Database.getMessages(this).get(id);
			messageSendingTask = new AsyncDialogSender(this);
			messageSendingTask.addListener(this);
			messageSendingTask.execute(sendMe);
			
		}
		
		return false;
		
	}
	
	public void setSporocilaCountText() { 
		naslov.setText(String.format("Čakajoča sporočila (%1$s)", Database.getMessages(this).size()));
	}
	
	public void refreshWaitingMessagesView() {
		listItems.invalidateViews();
		setSporocilaCountText();
	}

	@Override
	public void onTaskFinish() {
		refreshWaitingMessagesView();
	}
	
}
